/*
 * CadastrarVendedor.java
 *
 * Created on 10/06/2009, 08:53:34
 * Copyright LAS
 */
package Soligee.view;

import Soligee.controller.bo.ControleAcessoBO;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeBuscarImplements;
import Soligee.view.ApplicationBean1;
import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlCommandLink;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.paneltabset.PanelTabSet;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import Soligee.controller.facade.sessionFacadeImplements.SessionFacadeCadastrarImplements;
import Soligee.decorador.ValidaCamposObrigatorios;
import Soligee.model.vo.cadastros.UsuarioVO;
import Soligee.model.vo.cadastros.VendedorVO;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;

public class CadastrarVendedor extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    private int __placeholder;
    private boolean perm;
    Class classe = this.getClass();
    UsuarioVO user = new UsuarioVO();
    private int controle =0;
    private ValidaCamposObrigatorios valida = new ValidaCamposObrigatorios();
    
    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private HtmlInputText campoNomeCompleto = new HtmlInputText();

    public HtmlInputText getCampoNomeCompleto() {
        return campoNomeCompleto;
    }

    public void setCampoNomeCompleto(HtmlInputText hit) {
        this.campoNomeCompleto = hit;
    }
    private HtmlInputText campoComissaoPrazo = new HtmlInputText();

    public HtmlInputText getCampoComissaoPrazo() {
        return campoComissaoPrazo;
    }

    public void setCampoComissaoPrazo(HtmlInputText hit) {
        this.campoComissaoPrazo = hit;
    }
    private HtmlInputText campoComissaoVista = new HtmlInputText();

    public HtmlInputText getCampoComissaoVista() {
        return campoComissaoVista;
    }

    public void setCampoComissaoVista(HtmlInputText hit) {
        this.campoComissaoVista = hit;
    }
    private HtmlCommandButton btn_cadastrar = new HtmlCommandButton();

    public HtmlCommandButton getBtn_cadastrar() {
        return btn_cadastrar;
    }

    public void setBtn_cadastrar(HtmlCommandButton hcb) {
        this.btn_cadastrar = hcb;
    }
    private PopupBean panelPopup1Bean = new PopupBean();

    public PopupBean getPanelPopup1Bean() {
        return panelPopup1Bean;
    }

    public void setPanelPopup1Bean(PopupBean pb) {
        this.panelPopup1Bean = pb;
    }
    private PanelPopup painelPopUp = new PanelPopup();

    public PanelPopup getPainelPopUp() {
        return painelPopUp;
    }

    public void setPainelPopUp(PanelPopup pp) {
        this.painelPopUp = pp;
    }
    private HtmlCommandButton btn_fechaPopUp = new HtmlCommandButton();

    public HtmlCommandButton getBtn_fechaPopUp() {
        return btn_fechaPopUp;
    }

    public void setBtn_fechaPopUp(HtmlCommandButton hcb) {
        this.btn_fechaPopUp = hcb;
    }
    private HtmlOutputText textoPopUp = new HtmlOutputText();

    public HtmlOutputText getTextoPopUp() {
        return textoPopUp;
    }

    public void setTextoPopUp(HtmlOutputText hot) {
        this.textoPopUp = hot;
    }
    private HtmlCommandButton btnPopUp1 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp1() {
        return btnPopUp1;
    }

    public void setBtnPopUp1(HtmlCommandButton hcb) {
        this.btnPopUp1 = hcb;
    }
    private HtmlCommandButton btnPopUp2 = new HtmlCommandButton();

    public HtmlCommandButton getBtnPopUp2() {
        return btnPopUp2;
    }

    public void setBtnPopUp2(HtmlCommandButton hcb) {
        this.btnPopUp2 = hcb;
    }
    private PanelTabSet painelAbasVendedor = new PanelTabSet();

    public PanelTabSet getPainelAbasVendedor() {
        return painelAbasVendedor;
    }

    public void setPainelAbasVendedor(PanelTabSet pts) {
        this.painelAbasVendedor = pts;
    }
    private HtmlInputText campoCodVendedor1 = new HtmlInputText();

    public HtmlInputText getCampoCodVendedor1() {
        return campoCodVendedor1;
    }

    public void setCampoCodVendedor1(HtmlInputText hit) {
        this.campoCodVendedor1 = hit;
    }
    private HtmlCommandLink tx_opCancelarAba01 = new HtmlCommandLink();

    public HtmlCommandLink getTx_opCancelarAba01() {
        return tx_opCancelarAba01;
    }

    public void setTx_opCancelarAba01(HtmlCommandLink hcl) {
        this.tx_opCancelarAba01 = hcl;
    }

    // </editor-fold>
   
    public CadastrarVendedor() {
    }

    @Override
    public void init() {       
        super.init();
        this.inicializarCampos();
        this.limparCampos();
         // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("CadastrarVendedor Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    // </editor-fold>    
    }

    @Override
    public void preprocess() {
    }

    @Override
    public void prerender() {
    }

    @Override
    public void destroy() {
    }

    protected SessionBean1 getview$SessionBean1() {
        return (SessionBean1) getBean("view$SessionBean1");
    }

    protected RequestBean1 getview$RequestBean1() {
        return (RequestBean1) getBean("view$RequestBean1");
    }

    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public String btn_cadastrar_action() {
            valida.verificaCampoVazio(campoNomeCompleto);
            valida.verificaCampoVazio(campoComissaoPrazo);
            valida.verificaCampoVazio(campoComissaoVista);

            if(valida.verificaCampoVazio(campoNomeCompleto)||valida.verificaCampoVazio(campoComissaoPrazo)||valida.verificaCampoVazio(campoComissaoVista))
            controlaPopup(true,"Campos Obrigatórios!",false,true,"","Ok");
        
        else
            controlaPopup(true,"Deseja confirmar Cadastro?",true,true,"Sim","Não");
        
        return null;
    }

    protected CadastrarPessoaFisica getview$CadastrarPessoaFisica() {
        return (CadastrarPessoaFisica) getBean("view$CadastrarPessoaFisica");
    }

    protected BuscarPessoaFisica getview$BuscarPessoaFisica() {
        return (BuscarPessoaFisica) getBean("view$BuscarPessoaFisica");
    }

    protected ApplicationBean1 getview$ApplicationBean1() {
        return (ApplicationBean1) getBean("view$ApplicationBean1");
    }

    public String btnPopUp1_action() {
        if(controle ==0){
            SessionFacadeCadastrarImplements cadastrar = new SessionFacadeCadastrarImplements();
        try {
            cadastrar.cadastrarVendedor(Integer.parseInt(campoCodVendedor1.getValue().toString()),
                    campoNomeCompleto.getValue().toString(),
                    Double.parseDouble(campoComissaoPrazo.getValue().toString()),
                    Double.parseDouble(campoComissaoVista.getValue().toString()));

            controlaPopup(true,"Vendedor Cadastrado com Sucesso!",false,true,"","OK");
            inicializarCampos();

        } catch (Exception e) {

            controlaPopup(true,"Erro:" + e.getMessage() + "caused by: " + e.getStackTrace(),false,true,"","OK");

            System.err.println("Erro:" + e.getMessage() + "caused by: " + e.getStackTrace());
        }
        return null;
        }
        else
            if(controle ==1){
                
                return "voltar";
            }
        return null;
    }

    public void controlaPopup(boolean renderedPopup,String textoPoup,boolean btn1Rendered, boolean btn2Rendered,
            String textobtn1, String textobtn2){

        painelPopUp.setRendered(renderedPopup);
        textoPopUp.setValue(textoPoup);
        btnPopUp1.setValue(textobtn1);
        btnPopUp1.setRendered(btn1Rendered);
        btnPopUp2.setRendered(btn2Rendered);
        btnPopUp2.setValue(textobtn2);
    }

    public void inicializarCampos() {

        campoNomeCompleto.setMaxlength(50);
        //mostra o valor do id correto para inserção no banco
        painelPopUp.setRendered(false);
        SessionFacadeBuscarImplements facadeBusca = new SessionFacadeBuscarImplements();
        campoCodVendedor1.setValue(facadeBusca.totalDeRegistros(VendedorVO.class) + 1);
        //seta cor de fundo para inicializar em branco
        limparCampos();
        
    }

    public void limparCampos(){        
        this.campoComissaoPrazo.resetValue();
        this.campoNomeCompleto.resetValue();
        this.campoComissaoVista.resetValue();
    }

    public void tx_opCancelarAba_action() {
       this.limparCampos();
       
    }

    public String btnPopUp2_action() {
        painelPopUp.setRendered(false);
        return null;
    }

    public String panelLayoutLogo_action() {
        //return null means stay on the same page
        return "voltar";
    }
    
    
    //BotãoVoltar Superior
    public String btn_voltar_action() {
        controle = 1;
        controlaPopup(true,"Deseja Sair ?",true,true,"Sim","Não");     
        return null;
    }
}

